package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    // 处理部门查询分页请求 /department/list
    @RequestMapping("/list")
    @RequiredPermission(name="部门查询", expression = "department:list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list"; // /WEB-INF/views/department/list.ftl
    }


    // 处理删除请求 /department/delete?id=1
    @RequestMapping("/delete")
    @RequiredPermission(name="部门删除", expression = "department:delete")
    public String delete(Long id) {

        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list"; // localhost/department/list
    }

    // 处理保存或者修改请求 /department/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="部门编辑", expression = "department:saveOrUpdate")
    public String saveOrUpdate(Department department) {

        if (department.getId() == null) {
            departmentService.save(department);
        } else {
            departmentService.update(department);
        }
        return "redirect:/department/list"; // localhost/department/list
    }

}
